<?php
/**
 * 0.1 Asset a http request is http://demo.xpface.org/hello_world.html
 * 0.2 Apache will rewrite the URL to http://demo.xpface.org/xpface/index.php?page_id=hello_world
 *
 * 1. get $page_id, and then check the page and the related ui modules were already initiated
 * 1.1 IF NO THEN call the ParseTags.py, and output the middle files, sounds the php script source, into the path that be defined in the file {$myapp}.xpface.xml::dir/ui_module
 * 2. foreach(ui_modules)
 * 2.1 get the datasource config by ui_module's source path from {$myapp}.datasource.xml
 * 2.2 get data from the datasource and bind it to the key that be defined in above config file
 * 3. redo step 2 for page-level datasource
 * 4. set the return as $page_id, then return the control to the pworks framework
*/

require_once('pworks/mvc/action/BaseAction.class.php');
require_once "xpface/util/XPFaceConfigLoader.class.php";
require_once "xpface/util/XPFace.class.php";

class XPFaceAction extends BaseAction{
    
    /**
	 * @isnull false
	 */
    public $page_id;
    
    public function execute(){
        //xpface config
        $xpFaceConfigLoader = XPFaceConfigLoader::getInstance();
        $xpFaceConfig = $xpFaceConfigLoader->getXPFaceConfig();
        //var_dump($xpFaceConfig);
        
        //page config
        $pageConfig = $xpFaceConfigLoader->getPageConfig();
        //var_dump($pageConfig);
        
        //init tpl file
        $pythonCommand = $xpFaceConfig["python_command"]->value;
        $parseScript = $xpFaceConfig["parse_script"]->value;
        $sourceTplPath = $xpFaceConfig["source_tpl_path"]->value;
        $parsedTplPath = $xpFaceConfig["parsed_tpl_path"]->value;
        
        //parse tpl file
        $currentPageConfig = $pageConfig[$this->page_id];
        
        //template
        $sourceTplFile = $sourceTplPath . "/" . $currentPageConfig->template;
        $copiedSourceFile = $parsedTplPath . "/" . $currentPageConfig->template;
        $parsedTplFile = $parsedTplPath . "/" . $currentPageConfig->template . ".php";
        if (!file_exists($copiedSourceFile) || md5_file($copiedSourceFile) != md5_file($sourceTplFile)) {
            if (!file_exists($sourceTplFile)) {
                echo "File '{$parsedTplFile}' parse failing! ";
                exit;
            }
            copy($sourceTplFile, $copiedSourceFile);
            if (file_exists($parsedTplFile)) {
                unlink($parsedTplFile);
            }
            //var_dump("{$pythonCommand} {$parseScript} {$sourceTplFile} {$parsedTplFile}");
            $cmd = "{$pythonCommand} {$parseScript} {$sourceTplFile} {$parsedTplFile}";
            exec($cmd);
            if (!file_exists($parsedTplFile)) {
                echo "File '{$parsedTplFile}' parse failing! ";
                exit ;
            }
        }
        foreach ($currentPageConfig->regions as $regionId => $sourceUIModule) {
            $sourceTplFile = $sourceTplPath . "/" . $sourceUIModule;
            $copiedSourceFile = $parsedTplPath . "/" . $sourceUIModule;
            $parsedTplFile = $parsedTplPath . "/" . $sourceUIModule . ".php";
            if (file_exists($sourceTplFile) && file_exists($copiedSourceFile) && md5_file($copiedSourceFile) == md5_file($sourceTplFile)) {
                continue;
            }
            copy($sourceTplFile, $copiedSourceFile);
            if (file_exists($parsedTplFile)) {
                unlink($parsedTplFile);
            }
            //var_dump("python D:/python/system/ParseTags.py {$sourceTplFile} {$parsedTplFile}");
            $cmd = "{$pythonCommand} {$parseScript} {$sourceTplFile} {$parsedTplFile}";
            //var_dump($cmd);
            exec($cmd);
            if (!file_exists($parsedTplFile)) {
                echo "File '{$parsedTplFile}' parse failing! ";
                exit;
            }
        }
        //datasource
        
        $binds = array();
        foreach ($currentPageConfig->binds as $bindKey => $bindValue) {
            //var_dump($bindKey, $bindValue);
            $binds[$bindKey] = $bindValue;
        }
        $this->setData("binds", $binds);
        foreach ($currentPageConfig->regions as $regionId => $uimoduleSrc) {
            $dataSource = $xpFaceConfigLoader->getDataSourceConfig($uimoduleSrc);
            //var_dump($dataSource);
            if ($dataSource === null) {
                continue;
            }
            //var_dump($dataSource->data);
            $data = array();
            foreach ($dataSource->data as $key => $source) {
                $data[$key] = $source;
            }
            $this->setData(md5($dataSource->uimodule), $data);
        }
        return 'succ';
    }
}
